// 引入express服务器
const express = require('express');
// 创建路由器对象
const goods = express.Router();
// 引入连接池模块
const pool = require('../pool');

// ---------------1.查询一个商品信息------------------
goods.get("/details", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  let sql = 'SELECT lpk_goods.*,goods_count.* FROM (lpk_goods INNER JOIN goods_count ON lpk_goods.gid=goods_count.goods_id) WHERE gid=?';
  pool.query(sql, [obj.gid], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    if (result.length == 0) {
      res.send({
        code: 201,
        msg: '查询失败',
      });
    } else {
      res.send({
        code: 200,
        msg: '查询成功',
        data: result,
      });
    }
  })
})

// ---------------2.查询所有商品信息------------------
goods.get("/", (req, res, next) => {
  let sql = 'SELECT * FROM lpk_goods ORDER BY gid';
  pool.query(sql, (err, result) => {
    if (err) {
      next(err);
      return;
    }
    if (result.length == 0) {
      res.send({
        code: 201,
        msg: '查询失败',
      });
    } else {
      res.send({
        code: 200,
        msg: '查询成功',
        data: result,
      });
    }
  })
})
// 导出路由器对象
module.exports = goods;